package freespiritsocial.io.user.ui.fragment.mine

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgFollowsListBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.clearData
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ui.adapter.PeopleAdapter
import freespiritsocial.io.user.viewmodel.FollowsListViewModel
import freespiritsocial.io.user.viewmodel.GetMyFollowsViewModel
import freespiritsocial.io.user.viewmodel.SearchFriendViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @description 粉丝/关注 列表
 * @param isFollower 是否是粉丝
 */
class FollowsListFrg: BaseFragment<FollowsListViewModel,FrgFollowsListBinding>() {
    private val getMyFollowsViewModel by activityViewModels<GetMyFollowsViewModel>()
    private val searchFriendViewModel by viewModels<SearchFriendViewModel>()

    companion object {
        fun crate(isFollower: Boolean) =
            FollowsListFrg().apply {
                this.isFollower = isFollower
            }
    }

    var isFollower = false
    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        PeopleAdapter(isFollower = isFollower).apply {
            setOnItemChildClickListener { adapter,view,position ->
                val userInfo = data[position]
                when (view.id) {
                    R.id.tv_follow -> {
                        mViewModel.attention(userInfo.userId,userInfo.isFollowed)
                    }
                    R.id.tv_remove -> {
                        searchFriendViewModel.allData.remove(data[position])
                        removeAt(position)
                        mViewModel.attention(userInfo.userId,true)
                    }
                    R.id.iv_avatar -> {
                        searchFriendViewModel.getUserInfo(data[position].userId) {
                            nav().navigateAction(R.id.mineFrg,bundleOf(MineFrg.KEY_IS_USER to false,MineFrg.KEY_USER to it))
                        }
                    }
                }
            }
        }
    }

    override fun layoutId(): Int = R.layout.frg_follows_list

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        model = searchFriendViewModel
        click = Click()
        recyclerView.init(
            adapter,
            emptyImage = R.mipmap.empty_follows,
            emptyText = if (!isFollower) "No friends have been found yet." else "No one has followed you yet."
        ).addItemDecoration(SpaceItemDecoration(0,dp2px(15f)))

    }

    override fun createObserver() {
        super.createObserver()
        eventViewModel.attentionEvent.observe(this) { id ->
            if (isFollower.not()) {
                adapter.data.forEachIndexed { index,userInfo ->
                    if (userInfo.userId == id) {
                        userInfo.changeFollowStatus()
                        adapter.notifyItemChanged(index)
                        return@observe
                    }
                }
            }
        }
        //粉丝列表
        if (isFollower.not())
            mViewModel.getFans {
                adapter.setList(it)
                searchFriendViewModel.allData.clear()
                searchFriendViewModel.allData.addAll(adapter.data)
            }
        else {
            //关注列表
            lifecycleScope.launch {
                getMyFollowsViewModel.resultFollow.collect {
                    adapter.dealData(it)
                }
            }
        }
        lifecycleScope.launch {
            searchFriendViewModel.searchResultNetWork.collect {
                adapter.addData(it)
            }
        }
        searchFriendViewModel.searchText.observe(this@FollowsListFrg) {
            if (searchFriendViewModel.allData.isEmpty()) searchFriendViewModel.allData.addAll(adapter.data)
            adapter.clearData()
            searchFriendViewModel.search()
        }
    }

    inner class Click {
        fun removeSearch() {
            searchFriendViewModel.searchText.value = ""
        }
    }
}